package gold.digger;

import gold.vo.ListNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC382 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {

        private ListNode head;

        /**
         * @param head The linked list's head.
         *             Note that the head is guaranteed to be not null, so it contains at least one node.
         */
        public Solution(ListNode head) {
            this.head = head;
        }

        /**
         * Returns a random node's value.
         */
        public int getRandom() {
            Random random = new Random();
            ListNode cur = this.head;
            int val = cur.val, pos = 1;
            while (null != cur) {
                if (random.nextInt(pos) == pos - 1) {
                    val = cur.val;
                }
                ++pos;
                cur = cur.next;
            }

            return val;
        }

        private int[] sample(int[] nums, int n) {
            Random rd = new Random();
            int[] ans = new int[n];
            for (int i = 0; i < nums.length; i++) {
                if (i < n) {
                    ans[i] = nums[i];
                } else {
                    if (rd.nextInt(i + 1) < n) {
                        ans[rd.nextInt(n)] = nums[i];
                    }
                }
            }
            return ans;
        }
    }

    public void run() {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(random.nextInt(11));
        }

//        int[] nums = new int[]{1, 2, 3, 4, 5};
//        Solution solution = new Solution(null);
//        int[] ans = solution.sample(nums, 3);
//        for (int i = 0; i < ans.length; i++) {
//            System.out.printf("%d ", ans[i]);
//        }
    }

    public static void main(String[] args) throws Exception {
        LC382 an = new LC382();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
